package com.dongway.netcondition.socketToBox;

import java.net.Socket;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dongway.core.utils.ApplicationContextUtils;
import com.dongway.netcondition.model.DevNetState;
import com.dongway.netcondition.model.DevRecord;
import com.dongway.netcondition.model.OperationRecord;
import com.dongway.netcondition.service.DevRecordService;
import com.dongway.netcondition.service.OperationRecordService;

public class SocketContent {

  private static final Logger logger = LoggerFactory.getLogger(SocketContent.class);
	
    private Map<Integer, Socket> allScoket = new ConcurrentHashMap<Integer, Socket>();
    private Map<Integer, String> alldev = new ConcurrentHashMap<Integer, String>();
    private Map<Integer, SocketInfo> infoMap = new ConcurrentHashMap<Integer, SocketInfo>();
    private static SocketContent instance;


    public SocketContent() {}

    private DevRecordService devRecordService = ApplicationContextUtils
        .getBean(DevRecordService.class);

    private OperationRecordService operationRecordService = ApplicationContextUtils
        .getBean(OperationRecordService.class);


    public static SocketContent getInstance() {
        if (null == instance) {
            instance = new SocketContent();
        }
        return instance;
    }


    public void online(Integer id, Socket socket, String str) {

        // DevRecord devRecord =new DevRecord();
        OperationRecord operationRecord = new OperationRecord();
        // 记录设备上线日志
        if (allScoket.get(id) == null) {
             allScoket.put(id, socket);
//             devRecord.setId(id);
//             devRecord.setDevId(id);
//             devRecord.setTime(new Timestamp(System.currentTimeMillis()));
//             devRecord
//             .setToPnLocalState(Integer.parseInt(str.substring(0, 1)));
//             devRecord
//             .setToPnServerState(Integer.parseInt(str.substring(2, 3)));
//             devRecord
//             .setToVpnLocalState(Integer.parseInt(str.substring(4, 5)));
//             devRecord
//             .setToVpnServerState(Integer.parseInt(str.substring(6, 7)));
//             devRecordService.save(devRecord);

            operationRecord.setDevId(id);;
            operationRecord.setOperation("设备上线了");
            operationRecord.setTime(new Timestamp(System.currentTimeMillis()));
            operationRecordService.save(operationRecord);
        } else
            allScoket.put(id, socket);
    }


    public Socket getSocket(Integer id) {
        return allScoket.get(id);

    }


    public void putDev(Integer id, String str) {
        // 记录监控信息变化
        DevRecord devRecord = new DevRecord();
        if (alldev.get(id) == null || !alldev.get(id).equals(str)) {
            alldev.put(id, str);
            devRecord.setDevId(id);
            devRecord.setTime(new Timestamp(System.currentTimeMillis()));
            devRecord.setToPnServerState(Integer.parseInt(str.substring(0, 1)));
            devRecord.setToPnLocalState(Integer.parseInt(str.substring(2, 3)));
            devRecord.setToVpnServerState(Integer.parseInt(str.substring(4, 5)));
            devRecord.setToVpnLocalState(Integer.parseInt(str.substring(6, 7)));
            devRecordService.save(devRecord);
        }
        alldev.put(id, str);
    }


    public String getDev(Integer id) {
        return alldev.get(id);
    }


    public List<Integer> onlines() {
        return new ArrayList<Integer>(allScoket.keySet());
    }


    public DevNetState isOnline(Integer id) {
        DevNetState devNetState = new DevNetState();
        try {
            devNetState.setId(id);
            String dev = alldev.get(id);
            devNetState.setToPnServerState(Integer.parseInt(dev.substring(0, 1)));
            devNetState.setToPnLocalState(Integer.parseInt(dev.substring(2, 3)));
            devNetState.setToVpnServerState(Integer.parseInt(dev.substring(4, 5)));
            devNetState.setToVpnLocalState(Integer.parseInt(dev.substring(6, 7)));
            devNetState.setState(0);
            Socket socket = allScoket.get(id);
            socket.sendUrgentData(0XFF);
            logger.info("设备 ID:{}---在线", id);
        } catch (Exception e) {
            devNetState.setState(1);
            offLine(id);
            OperationRecord operationRecord = new OperationRecord();
            // 记录设备下线日志
            operationRecord.setDevId(id);;
            operationRecord.setOperation("设备连接下线了");
            operationRecord.setTime(new Timestamp(System.currentTimeMillis()));
            operationRecordService.save(operationRecord);
            logger.info("设备 ID:{}下线了", id);
        }
        return devNetState;
    }


    private void offLine(Integer id) {
        allScoket.remove(id);
        alldev.remove(id);
        infoMap.remove(id);
    }


    public static SocketInfo getInfo(Integer devId) {

        SocketInfo info = getInstance().infoMap.get(devId);
        if (null == info) {
            info = new SocketInfo();
            getInstance().infoMap.put(devId, info);
        }
        return getInstance().infoMap.get(devId);

    }

}
